import * as THREE from 'three'
import { huanJing } from '../huanJing/HuanJing.js'
import { wuLiWorker } from '../wuLi/WuLiWorker.js'
import { KongZhi } from './KongZhi.js'
import { ZiYouShiJiao } from './ZiYouShiJiao.js'
import { WanJiaKongZhi } from './WanJiaKongZhi.js'
import { JianPanShiJian } from './JianPanShiJian.js'
import { ShuBiaoShiJian } from './ShuBiaoShiJian.js'
import { ZhiZaoMoShi } from './ZhiZaoMoShi.js'
import { BianJiMoShi, bianJiMoShi } from './BianJiMoShi.js'

// 注册事件

const { camera, renderer } = huanJing

// 集中处理事件分发
function shiJianChuLi(shiJian) {
	for (let gongNeng of KongZhi.kongZhiMuBiao) {
		gongNeng.chuLi.call(gongNeng, shiJian)
	}
}

let zhongDuanQi = new AbortController() // 中断移除事件

let huaBu = huanJing.huaBu
huaBu.setAttribute('tabindex', '-1') // 使可以触发按键事件

function zhuCeShiJian() {
	window.navigator.keyboard?.lock() // 全屏后阻止事件传递给操作系统
	
	document.addEventListener("visibilitychange", yeMianXianShiYinCang)
	document.addEventListener('contextmenu', event => event.preventDefault())
	// 解决移动端pointermove区域问题，移动端滚动条detail等会失效，待解决
	// document.addEventListener('touchstart', event => event.preventDefault(), { passive: false })
	
	document.addEventListener("visibilitychange", shiJianChuLi)
	document.addEventListener("pointerlockchange", shiJianChuLi)
	huaBu.addEventListener('keydown', shiJianChuLi)
	huaBu.addEventListener('keyup', shiJianChuLi)
	huaBu.addEventListener('pointerdown', shiJianChuLi)
	huaBu.addEventListener('pointerup', shiJianChuLi)
	huaBu.addEventListener('pointermove', shiJianChuLi)
	huaBu.addEventListener('pointerlockchange', shiJianChuLi)
	huaBu.addEventListener('wheel', shiJianChuLi, { passive: false })
	
	let wanJiaKongZhi = new WanJiaKongZhi()
	let zhiZaoMoShi = new ZhiZaoMoShi()
	let ziYouShiJiao = new ZiYouShiJiao()
	let bianJiMoShi = new BianJiMoShi()
	let jianPanShiJian = new JianPanShiJian()
	// let shuBiaoShiJian = new ShuBiaoShiJian()
	
	wanJiaKongZhi.jiaZai()
	jianPanShiJian.jiaZai()
	// shuBiaoShiJian.jiaZai()
	
	huaBu.focus()
}
// 页面显示隐藏
function yeMianXianShiYinCang() {
	if (document.visibilityState !== "visible") wuLiWorker.tingZhiMoNi(true)
	else if (!bianJiMoShi.jiaZaiZhuangTai) wuLiWorker.tingZhiMoNi(false)
}

export {
	zhuCeShiJian
}